package com.uwo.house.kits;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by cynic on 2017/7/22.
 */
public class SmsKits {

    private static final Logger log = Logger.getLogger(SmsKits.class);

    private static final String addr = "http://api.sms.cn/sms/";
    private static final String userId = "lhgf18072977922";

    private static final String pwd = "9355dd75746ef284ade51e2cbb260a9d";

    private static final String encode = "utf8";

    public static String send(String msgContent, String mobile) throws Exception {
        //组建请求
        String straddr = addr +
                "?ac=send&uid="+userId+
                "&pwd="+pwd+
                "&mobile="+mobile+
                "&encode="+encode+
                "&content=" + msgContent;

        StringBuffer sb = new StringBuffer(straddr);
        log.warn("URL:"+sb);

        //发送请求
        URL url = new URL(sb.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream()));

        //返回结果
        String inputline = in.readLine();
        log.warn("Response:"+inputline);
        return inputline;
    }

}
